int GetStringHex(const char *hexString, char *outArray, int maxOutSize)
{
    int len = 0;
    char *s = hexString;
    int width = 0;
    char c = 0;

    if (!hexString) return -1;
    if (!outArray) return -2;
    if (maxOutSize <= 0) return-3;

    while (*s && *s <= ' ') s++; // skip useless characters

    while (*s)
    {
        if ('0' <= *s && *s <= '9')
        {
            c = (c << 4) | (*s - '0');
            width++;
        }
        else if ('a' <= *s && *s <= 'f')
        {
            c = (c << 4) | (*s - 'a' + 10);
            width++;
        }
        else if ('A' <= *s && *s <= 'F')
        {
            c = (c << 4) | (*s - 'A' + 10);
            width++;
        }
        else if (' ' == *s)
        {
            outArray[len++] = c;
            if (len >= maxOutSize) break;
            width = 0;
            c = 0;
            while (*s && *s <= ' ') s++; // skip useless characters
            continue;
        }
        else
        {
            return -4;
        }
        s++;
    }

    if (width > 0) outArray[len++] = c;

    return len;
}

int GetStringHex_s(char hexString[], int maxInSize, char outArray[], int maxOutSize)
{
    int len = 0;
    int i = 0;
    int width = 0;
    char c = 0;

    if (maxInSize <= 0) return -1;
    if (maxOutSize <= 0) return -2;

    while (i < maxInSize - 1 && hexString[i] != 0 && hexString[i] <= ' ') i++; // skip useless characters

    while (i < maxInSize && hexString[i] != 0)
    {
        if ('0' <= hexString[i] && hexString[i] <= '9')
        {
            c = (c << 4) | (hexString[i] - '0');
            width++;
        }
        else if ('a' <= hexString[i] && hexString[i] <= 'f')
        {
            c = (c << 4) | (hexString[i] - 'a' + 10);
            width++;
        }
        else if ('A' <= hexString[i] && hexString[i] <= 'F')
        {
            c = (c << 4) | (hexString[i] - 'A' + 10);
            width++;
        }
        else if (' ' == hexString[i])
        {
            outArray[len++] = c;
            if (len >= maxOutSize) break;
            width = 0;
            c = 0;
            while (i < maxInSize - 1 && hexString[i] != 0 && hexString[i] <= ' ') i++; // skip useless characters
            continue;
        }
        else
        {
            return -3;
        }

        i++;
    }

    if (width > 0) outArray[len++] = c;

    return len;
}